package com.cndw.kungfu;

import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;
import java.util.TimeZone;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.event.GameShutdownHandler;
import com.cndw.kungfu.event.GameStartHandler;
import com.cndw.kungfu.event.RequestHandler;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.model.Context;
import com.cndw.rpg.framework.codec.AmfMessageEncode;
import com.cndw.rpg.framework.socket.GameServer;

public class StartServer { 

    private static StartServer startServer;
    
    public static void main(String[] args) {
		
		try {
			getInstance().start();			
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
    
    public static StartServer getInstance(){
    	if (startServer == null) {
    		synchronized (StartServer.class) {
				if (startServer == null) {
					startServer = new StartServer();
				}
			}    		
		}    	
    	return startServer;
    }
 
	public void init() throws Exception {
		System.out.println("execute init method !");
	}

	public void init(String[] args) throws Exception {
		System.out.println("execute init(args) method");
	}

	public void start() throws Exception {
		LogUtil.getMain().info("Server is starting ... ");       
		Properties properties = new Properties();
		properties.load(new InputStreamReader(StartServer.class.getResourceAsStream("/system.properties"),"UTF-8"));
		System.setProperty("log4jdir", properties.getProperty("log4jdir"));
		//start spring
		new ClassPathXmlApplicationContext("game.xml");
		//start game socket-server
        int port = properties.containsKey("ServerPort")?
                    Integer.valueOf(properties.get("ServerPort").toString()):8080;
        //AmfMessageDecode.context.legacyCollection = true;
        AmfMessageEncode.context.legacyCollection = true;//将list映射成数组
        //AmfMessageEncode.context.supportRemoteClass = true;
        	
        ConstantsConfig.load();        
        boolean sysClosed = ConstantsConfig.isSysClosed();
        ConstantsConfig.setSysClosed(true);
        ConstantsSystem.setCloseState(1);//设置服务器关闭状态 1启动了任务 2 开始了 4 完成了
        
        GameServer server = new GameServer(port);
        server.setRequestHandler(new RequestHandler());
		server.start();		
		LogUtil.getMain().info("Listen on port "+port);  
        
        StringBuilder sb = new StringBuilder();
        sb.append("partnerEName:").append(ConstantsConfig.getSysCgPartnerEName());
        sb.append(",serviceId:").append(ConstantsConfig.getSysCgServiceId());
        sb.append(",svrId:").append(ConstantsConfig.getSysCgSvrId());
        sb.append(",autoNumberSuffix:").append(ConstantsConfig.getSysCgAutoNumberSuffix());
        sb.append(",worldId:").append(ConstantsConfig.getSysCgWorldId());        
        sb.append(",payRate:").append(ConstantsConfig.getSysCgPayRate());
        sb.append(",timeZone:").append(TimeZone.getDefault().getID());
        sb.append(",charset:").append(Charset.defaultCharset().displayName());
        
        
        LogUtil.getMain().info("args->"+sb.toString());
        
        
        
        GameShutdownHandler hook = new GameShutdownHandler();
        hook.setState(1);//设置为关闭状态        
        Runtime.getRuntime().addShutdownHook(hook);
        
        Runnable r = new GameStartHandler(sysClosed);
        Context.getTimerServer().getScheduledExecutor().execute(r);
        LogUtil.getMain().info("Server start success");
    }

	public void stop() throws Exception {
		System.out.println("execute stop method!");
	}

	public void destroy() throws Exception {
		System.out.println("execute destroy method!");
	}
}